package com.air.controller;

import com.air.config.Result;
import com.air.entity.Card;
import com.air.entity.User;
import com.air.exception.ParamsErrorException;
import com.air.service.UserService;
import com.air.util.RegularUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.HttpCookie;

/**
 * 用户
 *
 * @author 彭程
 */
@Controller
public class UserController {

    @Autowired
    UserService userService;


    @RequestMapping(value = "/getName")
    @ResponseBody
    public Result getName(HttpSession session){
        Object id = session.getAttribute("id");
        if (id == null){
            return Result.fail("未登录");
        }else{
            User user = userService.selectUserById((Long) id);

            return Result.success("登陆成功",user.getFirstName()+user.getLastName());
        }
    }

    /**
     * 登录
     *
     * @param openId 账号
     * @param password 密码
     * @return 结果
     */
    @RequestMapping(value = "/signIn")
    @ResponseBody
    public Result signIn(@RequestParam("openId") String openId,
                         @RequestParam("password") String password,
                         HttpSession session,
                         HttpServletResponse response) {

        boolean b = false;
        String phone ;
        if ("".equals(openId) || "".equals(password)) {
            throw new ParamsErrorException("参数错误");
        }
        if (RegularUtil.checkEmail(openId)) {
            System.out.println("邮箱登录"+openId);
            b = userService.loginByEmail(openId, password);
            if (b){
                User user = userService.selectUserByEmail(openId);
                session.setAttribute("phone", user.getPhone());
                session.setAttribute("id", user.getId());
                return Result.success("登陆成功"+user.getId(), null);
            }

        } else if (RegularUtil.checkIdNumber(openId)) {
            System.out.println("身份号"+openId);
            b = userService.loginByIdNumber(openId, password);
            if (b){
                User user = userService.selectUserByCardId(openId);
                session.setAttribute("phone", user.getPhone());
                session.setAttribute("id", user.getId());
                return Result.success("登陆成功", null);
            }

        } else if (RegularUtil.checkPhoneNumber(openId)) {
            System.out.println("电话号"+openId);
            b = userService.loginByPhone(openId, password);
            if (b){
                User user = userService.selectUserByPhone(openId);
                System.out.println(user);
                session.setAttribute("phone", user.getPhone());
                session.setAttribute("id", user.getId());
                return Result.success("登陆成功", null);
            }

        } else {
            System.out.println("?????wtf???");
            return Result.fail("登录失败->请正确输入内容");
        }

        return Result.fail("登录失败->账号或密码错误");

    }


    @RequestMapping("/signOut")
    @ResponseBody
    public Result signOut(HttpSession session) {
        session.invalidate();
        return Result.success("退出登录",null);
    }

    @RequestMapping(value = "/signInBySms")
    @ResponseBody
    public Result signInBySms(@RequestParam("phone") String phone,
                              @RequestParam("code") String code,
                              HttpSession session,
                              HttpServletResponse response) {
        String sessionCode = (String) session.getAttribute(phone);
        if (StringUtils.isNotEmpty(sessionCode)) {
            if (sessionCode.equals(code)) {
                System.out.println("验证码验证通过");
                User user = userService.selectUserByPhone(phone);
                if (user != null) {
                    session.setAttribute("phone", phone);
                    return Result.success("登陆成功", null);
                } else {
                    return Result.fail("登录失败->当前用户不存在");
                }
            } else {
                return Result.fail("登录失败->验证码不正确");
            }
        } else {
            return Result.fail("登录失败->验证码超时");
        }
    }


    @RequestMapping(value = "/resign")
    @ResponseBody
    public Result resign(User user, Card card) {
        System.out.println(user);
        System.out.println(card);
        user.setCard(card);
        boolean b = userService.insertUser(user, card);
        if (b) {
            return Result.success("注册成功", null);
        } else {
            return Result.fail("注册失败->当前用户已存在");
        }
    }
}
